﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AutomationTools;
using AutomationTools.Procedimentos;
using AutomationTools.Procedimentos.Acao.Transformacao;
using System.Collections;
using AutomationTools.Procedimentos.Busca;
using AutomationTools.Procedimentos.Iteracao;
using AutomationTools.Procedimentos.Leitura;

namespace AutomationToolsSample
{
    public partial class Form4 : Form
    {
        public Form4()
        {
            InitializeComponent();
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            ParametrosRequest nill = new ParametrosRequest();
            nill.Add(new ParametroRequest("groups_id", "1"));
            nill.Add(new ParametroRequest("keywords", txtPesquisar.Text));

            ProcedimentoController controller = new ProcedimentoController(
                new Request("http://www.mtgmintcard.com/mtg_search_result.php", nill, Request.EnumFormMethod.GET)
                , new EncontrarTagHTMLByXPath("//table[@class='resultList']/tr[position() > 1]")
                , new ParaCada(new RegexParseObject<ResultadoMint>())
            );

            var result = ((IEnumerable)controller.ExecutarProcedimentos()).Cast<ResultadoMint>().SelectMany(x => x.Cards);

            label1.Text = "";
            foreach (var cards in result)
            {
                label1.Text += cards.Nome + "-->" + cards.Preco + Environment.NewLine;
            }
        }


        [CanRegexParse]
        public class ResultadoMint
        {
            [RegexParse(@"<tr[^>]*>.*?</tr>", IsArray = true)]
            public CartaMint[] Cards { get; set; }
        }

        [CanRegexParse]
        public class CartaMint
        {
            [RegexParse(@"<td.*?>.*?alt=""([^""]+)"".*?</td>", Grupo = 1, Index = 0)]
            public string Nome { get; set; }

            [RegexParse(@"<td.*?>.*?alt=""([^""]+)"".*?</td>", Grupo = 1, Index = 1)]
            public string Colecao { get; set; }

            [RegexParse(@"<td.*?>.*?alt=""([^""]+)"".*?</td>", Grupo = 1, Index = 2)]
            public string Modo { get; set; }

            [RegexParse(@"symbols/((?:(?!symbol).)*)_mana.gif.*?alt=""([\D])""", Grupo = 1)]
            public string Cor { get; set; }

            [RegexParse(@"symbols/((?:(?!symbol).)*)_mana.gif.*?alt=""([\D])""", Grupo = 2)]
            public string Raridade { get; set; }

            [RegexParse(@"<span.*?>\$(.*?)</span>", Grupo = 1)]
            public string Preco { get; set; }
        }
    }
}
